import java.io.*;

import org.jibble.pircbot.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * SPUSTACIA CLASSA
 * 
 * @author harph
 * 
 */
public class Run {
	static Shalatik bot;
	static QOGThread qog;
	static RespawnThread respawn;
	static RegenThread regen;

	public static void main(String[] args) throws IOException {
		try {
			// The newInstance() call is a work around for some
			// broken Java implementations
			System.out.println("loading jdbc");
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			System.out.println("jdbc loaded");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		try {
			Properties config = new Properties();

			try {
				config.load(new FileInputStream("config.ini"));
			} catch (FileNotFoundException ex) {
				File configFile = new File("config.ini");
				Writer content = new BufferedWriter(new FileWriter(configFile));

				content.write("# server settings\n");
				content.write("\n");
				content.write("server = irc.nextra.sk\n");
				content.write("channel = #fiit\n");
				content.write("nick = shalat\n");
				content.write("\n");
				content.write("# koeficienty\n");
				content.write("\n");
				content.write("strkoef1 = 3\n");
				content.write("strkoef2 = 1.2\n");
				content.write("intkoef1 = 1.5\n");
				content.write("intkoef2 = 4\n");
				content.write("agikoef1 = 2\n");
				content.write("agikoef2 = 2\n");
				content.write("hpkoef = 100\n");
				content.write("manakoef = 80\n");
				content.write("\n");
				content.write("# intervaly (v milisekundach 1 s = 1000 ms)\n");
				content.write("\n");
				content.write("respawninterval = 60000\n");
				content.write("regeninterval = 5000\n");

				content.close();

				config.load(new FileInputStream("config.ini"));
			}

			String ircServer = config.getProperty("server");
			String ircChannel = config.getProperty("channel");
			String ircNick = config.getProperty("nick");

			float strkoef1 = Float.parseFloat(config.getProperty("strkoef1"));
			float strkoef2 = Float.parseFloat(config.getProperty("strkoef2"));
			float intkoef1 = Float.parseFloat(config.getProperty("intkoef1"));
			float intkoef2 = Float.parseFloat(config.getProperty("intkoef2"));
			float agikoef1 = Float.parseFloat(config.getProperty("agikoef1"));
			float agikoef2 = Float.parseFloat(config.getProperty("agikoef2"));
			float hpkoef = Float.parseFloat(config.getProperty("hpkoef"));
			float manakoef = Float.parseFloat(config.getProperty("manakoef"));
			float strstaminakoef = Float.parseFloat(config.getProperty("strstaminakoef"));
			float intstaminakoef = Float.parseFloat(config.getProperty("intstaminakoef"));
			float agistaminakoef = Float.parseFloat(config.getProperty("agistaminakoef"));
			float strstaminaregen = Float.parseFloat(config.getProperty("strstaminaregen"));
			float intstaminaregen = Float.parseFloat(config.getProperty("intstaminaregen"));
			float agistaminaregen = Float.parseFloat(config.getProperty("agistaminaregen"));
			float stamina_attack = Float.parseFloat(config.getProperty("stamina_attack"));

			int respawninterval = Integer.parseInt(config
					.getProperty("respawninterval"));
			int regeninterval = Integer.parseInt(config
					.getProperty("regeninterval"));

			System.out.println("connecting to db");
			Connection con = DriverManager
					.getConnection("jdbc:mysql://localhost/shalatikDB?user=shalatik&password=Hpp7fF2KySsK52DY");
			// Connection con =
			// DriverManager.getConnection("jdbc:mysql://127.0.0.1/harph?user=harph&password=One11onE");
			System.out.println("connected");
			// Do something with the Connection
			// koef 1 prislucha hp a 2 mane

			String chan = ircChannel;
			String nick = ircNick;
			// //////////////////////////////////////////////////////vyhodit do
			// konfigu!!!!!
			bot = new Shalatik(con, nick, strkoef1, strkoef2,
					intkoef1, intkoef2, agikoef1, agikoef2, hpkoef, manakoef,
					strstaminakoef, intstaminakoef, agistaminakoef, stamina_attack);
			ControlThread ct = new ControlThread();
			ct.start();
			qog = new QOGThread(con, bot, chan);
			qog.start();

			respawn = new RespawnThread(con, bot, chan, strkoef1, strkoef2,
					intkoef1, intkoef2, agikoef1, agikoef2, hpkoef, manakoef,
					respawninterval, strstaminakoef, agistaminakoef, intstaminakoef);
			respawn.start();
			regen = new RegenThread(con, bot, chan, strkoef1, strkoef2,
					intkoef1, intkoef2, agikoef1, agikoef2, hpkoef, manakoef,
					regeninterval, strstaminakoef, intstaminakoef, agistaminakoef,
					strstaminaregen, intstaminaregen, agistaminaregen);
			regen.start();

			// Enable debugging output.
			bot.setVerbose(true);

			// Connect to the IRC server.
			try {
				// bot.connect("irc.gts.cz");
				bot.connect(ircServer);
				bot.joinChannel(ircChannel);
				// bot.connect("irc.quakenet.org");
				// bot.joinChannel("#KalUnderground");

			} catch (IOException e1) {
				System.out.println("debug: IOException");
				e1.printStackTrace();
			} catch (IrcException e2) {
				System.out.println("debug: IrcException");
				e2.printStackTrace();
			}

		} catch (SQLException ex) {
			// handle any errors
			System.out.println("SQLException: " + ex.getMessage());
			System.out.println("SQLState: " + ex.getSQLState());
			System.out.println("VendorError: " + ex.getErrorCode());
		}

	}
}
